<?php
/**
 * School_Room Controller
 *
 * School_Room Management
 *
 *
 * @version $Id: RoomController.php 77 2010-04-25 07:28:48Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package school
 * @subpackage Controller
 */
class School_RoomController extends Zend_Controller_Action
{

    function indexAction()
    {
        $q = Doctrine_Query::create()
        ->from('School_Model_SchoolRoom s')
        ->where('s.enable = ?','Y')
        ->orderBy('s.rank');

        $this->view->data = $q->fetchArray();

        $schoolRoom = new  School_Model_SchoolRoom();

        // 取得處室下的職稱數
        $this->view->title_count =   $schoolRoom->getParentCount();
    }

    function editAction()
    {
        if ($this->_request->isPost()) {

            $name = $this->_request->getParam('name');
            $phone = $this->_request->getParam('phone');
            $phone_branch = $this->_request->getParam('phone_branch');
            $memo = $this->_request->getParam('memo');
            $rank = $this->_request->getParam('rank');

            $schoolRoom = new School_Model_SchoolRoom();
            foreach ($name as $sn => $val) {
                if ($val == '') continue;
                $schoolRoom->assignIdentifier($sn);
                $schoolRoom->name = $val;
                $schoolRoom->phone = $phone[$sn];
                $schoolRoom->phone_branch = $phone_branch[$sn];
                $schoolRoom->rank = $rank[$sn];
                $schoolRoom->memo = $memo[$sn];
                $schoolRoom->enable = 'Y';
                $schoolRoom->save();
            }
        }
        $this->_redirect('school/room');
    }


    function deleteAction()
    {
        $sn = (int)$this->_request->getParam('sn',0);
        $schoolRoom = Doctrine::getTable('School_Model_SchoolRoom')
        ->findOneBy('sn',$sn);
        if ($schoolRoom) {
            $schoolRoom->delete();
        }
         $this->_redirect('school/room');
    }


    function addAction()
    {
        $name = $this->_request->getParam('name');
        if ($this->_request->isPost() and $name<>'') {

            $schoolRoom = new School_Model_SchoolRoom();
            $phone = $this->_request->getParam('phone');
            $phone_branch = $this->_request->getParam('phone_branch');
            $memo = $this->_request->getParam('memo');
            $rank = $this->_request->getParam('rank');

            $schoolRoom->name = $name;
            $schoolRoom->phone = $phone;
            $schoolRoom->phone_branch = $phone_branch;
            $schoolRoom->rank = $rank;
            $schoolRoom->memo = $memo;
            $schoolRoom->enable = 'Y';
            $schoolRoom->save();

        }
        $this->_redirect('school/room/index');
    }

}